home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / os2 / os2_hpfs.zip / OS2-HPFS.TXT
Text File  |  1993-02-16  |  40KB  |  668 lines

  1.  
  2. Here's a technical description of the High Performance Files System
  3. that was posted on the net recently.
  4.  
  5.  *****  Computer Library Periodicals, Jan 1990 : Doc #14753  *****
  6.  
  7. Journal:   Microsoft Systems Journal  Sept 1989  v4 n5 p1(13)
  8.            * Full Text COPYRIGHT Microsoft Corp. 1989.
  9. -----------------------------------------------------------------------------
  10. Title:     Design goals and implementation of the new High Performance File
  11.            System. (includes related article on B-Trees and B+ Trees)
  12. Author:    Duncan, Roy.
  13.  
  14. Summary:   The High Performance File System (HPFS) enhancement to OS/2
  15. Version 1.2 solves all the problems of the File Allocation Table (FAT) file
  16. system and is designed to meet the demands expected into the next few
  17. decades.  HPFS not only serves as a way to organize data on random access
  18. block storage devices, but is also a software module that translates
  19. file-oriented requests from applications programs to device drivers.  HPFS is
  20. also an example of an installable file system, which makes it possible to
  21. access several incompatible volume structures on the same OS/2 system
  22. simultaneously.  Excellent throughput is achieved by the use of advanced data
  23. structures such as intelligent caching, read-ahead and write-behind.  Disk
  24. space is managed more economically by the use of sectoring.  HPFS also
  25. includes greatly improved fault tolerance.  Applications programs need only
  26. simple modifications to make use of extended attributes and long filenames.
  27. -----------------------------------------------------------------------------
  28. Descriptors..
  29. Product:   OS-2 Extended Edition 1.2 (Operating system) (product
  30.            enhancement).
  31. Topic:     OS-2
  32.            File Management
  33.            Enhancements
  34.            Data Structures
  35.            Disk Space Allocation
  36.            Sectoring.
  37. Feature:   illustration
  38.            table
  39.            chart.
  40. Caption:   (Comparison of FAT and High Performance File System.)
  41.            (Overall structure of an HPFS volume.)
  42.            (Overall structure of an Fnode.)
  43.  
  44. Record#:   07 585 454.
  45. -----------------------------------------------------------------------------
  46. Full Text:
  47.  
  48. THE HPFS IS A WAY OF ORGANIZING DATA ON A RANDOM ACCESS BLOCK STORAGE DEVICE.
  49. IT IS ALSO A SOFTWARE MODULE THAT TRANSLATES FILE-ORIENTED REQUESTS FROM AN
  50. APPLICATION PROGRAM INTO MORE PRIMITIVE REQUESTS THAT A DEVICE DRIVER CAN
  51. UNDERSTAND.
  52.  
  53. The High Performance File System (hereafter HPFS), which is making its first
  54. appearance in the OS/2 operating systemVersion 1.2, had its genesis in the
  55. network division of Microsoft and was designed by Gordon Letwin, the chief
  56. architect of the OS/2 operating system.  The HPFS has been designed to meet
  57. the demands of increasingly powerful PCs, fixed disks, and networks for many
  58. years to come and to serve as a suitable platform for object-oriented
  59. languages, applications, and user interfaces.
  60.  
  61. The HPFS is a complex topic because it incorporates three distinct yet
  62. interrelated file system issues.  First, the HPFS is a way of organizing data
  63. on a random access block storage device. Second, it is a software module that
  64. translates file-oriented requests from an application program into more
  65. primitive requests that a device driver can understand, using a variety of
  66. creative techniques to maximize performance.  Third, the HPFS is a practical
  67. illustration of an important new OS/2 feature known as installable file
  68. systems.
  69.  
  70. This article introduces the three aspects of the HPFS.  But first, it puts
  71. the HPFS in perspective by reviewing some of the problems that led to the
  72. system's existence.
  73.  
  74. FAT File System
  75.  
  76. The so-called FAT file system, which is the file system used in all versions
  77. of the MS-DOS"' operating system to date and in the first two releases of
  78. OS/2' (Versions 1.0 and 1.1), has a dual heritage in Microsoft's earliest
  79. programming language products and the Digital Research(R) CP/M(R) operating
  80. system-software originally written for 8080-based and Z-80-based
  81. microcomputers.  It inherited characteristics from both ancestors that have
  82. progressively tumed into handicaps in this new era of multitasking, protected
  83. mode, virtual memory, and huge fixed disks.
  84.  
  85. The FAT file system revolves around the File Allocation Table for which it is
  86. named.  Each logical volume has its own FAT, which serves two important
  87. functions: it contains the allocation information for each file on the volume
  88. in the form of linked lists of allocation units (clusters, which are
  89. power-of-2 multiples of sectors) and it indicates which allocation units are
  90. free for assignment to a file that is being created or extended.
  91.  
  92. The FAT was invented by Bill Gates and Marc McDonald in1977 as a method of
  93. managing disk space in the NCR version of standalone Microsoft* Disk BASIC.
  94. Tim Paterson, at that time an employee of Seattle (R) Computer Products
  95. (SCP), was introduced to the FAT concept when his company shared a booth with
  96. Microsoft at the National Computer Conference in 1979.  Paterson subsequently
  97. incorporated FATs into the file system of 86-DOS, an operating system for
  98. SCP's S-100 bus 8086 CPU boards.  86-DOS was eventually purchased by
  99. Microsoft and became the starting point for MS-DOS2 Version 1.0, which was
  100. released for the original IBM" PC in August 1981.
  101.  
  102. When the FAT was conceived, it was an excellent solution to disk management,
  103. mainly because the floppy disks on which it was used were rarely larger than
  104. I Mb.  On such di s, the FAT was small enough to be held in memory at all
  105. times, allowing very fast random access to any part of any file.  This proved
  106. far superior to the CP/M method of tracking disk space, in which the
  107. information about the sectors assigned to a file might be spread across many
  108. directory entries, which were in turn scattered randomly throughout the disk
  109. directory.
  110.  
  111. When applied to fixed disks, however, the FAT began to look more like a bug
  112. than a feature.  It became too large to be held entirely resident and had to
  113. be paged into memory in pieces; this paging resulted in many superfluous disk
  114. head movements as a program was reading through a file and degraded system
  115. throughput.  In addition, because the information about free disk space was
  116. dispersed across many sectors of FAT, it was impractical to allocate file
  117. space contiguously, and file fragmentation became another obstacle to good
  118. performance.  Moreover, the use of relatively large clusters on fixed disks
  119. resulted in a lot of dead space, since an average of one-half cluster was
  120. wasted for each file.  (Some network servers use clusters as large as 64Kb.)
  121.  
  122. The FAT file system's restrictions on naming files and directories are
  123. inherited from CP/M. When Paterson was writing 86DOS, one of his primary
  124. objectives was to make programs easy to port from CP/M to his new operating
  125. system.  He therefore adopted CP/M's limits on filenames and extensions so
  126. the critical fields of 86-DOS File Control Blocks (FCBs) would look almost
  127. exactly like those of CP/M.  The sizes of the FCB filename and extension
  128. fields were also propagated into the structure of disk directory entries. In
  129. due time, 86-DOS became MSDOS and application programs for MS-DOS
  130. proliferated beyond anyone's wildest dreams.  Since most of the early
  131. programs depended on the structure of FCBs, the 8.3 format for filenames
  132. became irrevocably locked into the system.
  133.  
  134. During the last couple of years, Microsoft and IBM have made valiant attempts
  135. to prolong the useful life of the FAT file system by lifting the restrictions
  136. on volume sizes, improving allocation strategies, caching pathnames, and
  137. moving tables and buffers into expanded memory.  But these can only be
  138. regarded as temporizing measures, because the fundamental data structures
  139. used by the FAT file system are simply not well suited to large random access
  140. devices.
  141.  
  142. The HPFS solves the FAT file system problems mentioned here and many others,
  143. but it is not derived in any way from the FAT file system.  The architect of
  144. the HPFS started with a clean sheet of paper and designed a file system that
  145. can take full advantage of a multitasking environment, and that will be able
  146. to cope with any sort of disk device likely to arrive on microcomputers
  147. during the next decade.
  148.  
  149. HPFS Volume Structure
  150.  
  151. HPFS volumes are a new partition type-type 7-and can exist on a fixed disk
  152. alongside of the several previously defined FAT partition types.
  153. IBM-compatible HPFS volumes use a sector size of 512 bytes and have a maximum
  154. size of 2199Gb (2" sectors).  Although there is no particular reason why
  155. floppy disks can't be formatted as HPFS volumes, Microsoft plans to stick
  156. with FAT file systems on floppy disks for the foreseeable future.  (This
  157. ensures that users will be able to transport files easily between MS-DOS and
  158. OS/2 systems.)
  159.  
  160. An HPFS volume has very few fixed structures (Figure 1).  Sectors 0-15 of a
  161. volume (8Kb) are the BootBlock and contain a volume name, 32-bit volume ID,
  162. and a disk bootstrap program.  The bootstrap is relatively sophisticated (by
  163. MS-DOS standards) and can use the HPFS in a restricted mode to locate and
  164. read the operating system files wherever they might be found.
  165.  
  166. Sectors 16 and 17 are known as the SuperBlock and the SpareBlock
  167. respectively.  The SuperBlock is only modified by disk maintenance utilities.
  168. It contains pointers to the free space bitmaps, the bad block list, the
  169. directory block band, and the root directory.  It also contains the date that
  170. the volume was last checked out and repaired with CHKDSK/F.  The SpareBlock
  171. contains various flags and pointers that will be discussed later; it is
  172. modified, although infrequently, as the system executes.
  173.  
  174. The remainder of the disk is divided into 8Mb bands.  Each band has its own
  175. free space bitmap in which a bit represents each sector.  A bit is 0 if the
  176. sector is in use and I if the sector is available.  The bitmaps are located
  177. at the head or tail of a band so that two bitmaps are adjacent between
  178. alternate bands.  This allows the maximum contiguous free space that can be
  179. allocated to a file to be 16Mb.  One band, located at or toward the seek
  180. center of the disk, is called the directory block band and receives special
  181. treatment (more about this later).  Note that the band size is a
  182. characteristic of the current implementation and may be changed in later
  183. versions of the file system.
  184.  
  185. Files and Fnodes
  186.  
  187. Every file or directory on an HPFS volume is anchored on a fundamental file
  188. system object called an Fnode (pronounced "eff node").  Each Fnode occupies a
  189. single sector and contains control and access history information used
  190. internally by the file system, extended attributes and access control lists
  191. (more about this later), the length and the first 15 characters of the name
  192. of the associated file or directory, and an allocation structure (Figure 2).
  193. An Fnode is always stored near the file or directory that it represents.
  194.  
  195. The allocation structure in the Fnode can take several forms, depending on
  196. the size and degree of contiguity of the file or directory.  The HPFS views a
  197. file as a collection of one or more runs or extents of one or more contiguous
  198. sectors.  Each run is symbolized by a pair of doublewords-a 32-bit starting
  199. sector number and a 32-bit length in sectors (this is referred to as
  200. runlength encoding).  From an application program's point of view, the
  201. extents are invisible; the file appears as a seamless stream of bytes.
  202.  
  203. The space reserved for allocation information in an Fnode can hold pointers
  204. to as many as eight runs of sectors of up to 16Mb each.  (This maximum run
  205. size is a result of the band size and free space bitmap placement only; it is
  206. not an inherent limitation of the file system.) Reasonably small files or
  207. highly contiguous files can therefore be described completely within the
  208. Fnode (Figure 3).
  209.  
  210. HPFS uses a new method to represent the location of files that are too large
  211. or too fragmented for the Fnode and consist of more than eight runs.  The
  212. Fnode's allocation structure becomes the root for a B+ Tree of allocation
  213. sectors, which in tum contain the actual pointers to the file's sector runs
  214. (see Figure 4 and the sidebar, "BTrees and B+ Trees").  The Fnode's root has
  215. room for 12 elements. Each allocation sector can contain, in addition to
  216. various control information, as many as 40 pointers to sector runs.
  217. Therefore, a two-level allocation B+ Tree can describe a file of 480 (12*40)
  218. sector runs with a theoretical maximum size of 7.68Gb (12*40*16Mb) in the
  219. current implementation (although the 32-bit signed offset parameter for
  220. DosChgFilePtr effectively limits file sizes to 2Gb).
  221.  
  222. In the unlikely event that a two-level B+ Tree is not sufficient to describe
  223. a highly fragmented file, the file system will introduce additional levels in
  224. the tree as needed.  Allocation sectors in the intermediate levels can hold
  225. as many as 60 internal (nonterminal) B+ Tree nodes, which means that the
  226. descriptive ability of this structure rapidly grows to numbers that are
  227. nearly beyond comprehension.  For example, a threelevel allocation B+ Tree
  228. can describe a file with as many as 28,800 (12*60*40) sector runs.
  229.  
  230. Run-length encoding and B+ Trees of allocation sectors are a memory-efficient
  231. way to specify a file's size and location, but they have other significant
  232. advantages.  Translating a logical file offset into a sector number is
  233. extremely fast: the file system just needs to traverse the list (or B+ Tree
  234. of lists) of run pointers until it finds the correct range. It can then
  235. identify the sector within the run with a simple calculation.  Run-length
  236. encoding also makes it trivial to extend the file logically if the newly
  237. assigned sector is contiguous with the file's previous last sector; the file
  238. system merely needs to increment the size doubleword of the file's last run
  239. pointer and clear the sector's bit in the appropriate freespace bitmap.
  240.  
  241. Directories
  242.  
  243. Directories, like files, are anchored on Fnodes.  A pointe to the Fnode for
  244. the root directory is found in the SuperBlock.  The Fnodes for directories
  245. other than the root are reache through subdirectory entries in their parent
  246. directories.
  247.  
  248. Directories can grow to any size and are built up from 2Kb directory blocks,
  249. which are allocated as four consecutive sectors on the disk.  The file system
  250. attempts to allocate directory blocks in the directory band, which is located
  251. at or near the seek center of the disk.  Once the directory band is full, the
  252. directory blocks are allocated wherever space is available.
  253.  
  254. Each 2Kb directory block contains from one to many directory entries.  A
  255. directory entry contains several fields, including time and date stamps, an
  256. Fnode pointer, a usage count for use by disk maintenance programs, the length
  257. of the file or directory name, the name itself, and a B-Tree pointer.  Each
  258. entry begins with a word that contains the length of the entry.  This
  259. provides for a variable amount of flex space at the end of each entry, which
  260. can be used by special versions of the file system and allows the directory
  261. block to be traversed very quickly (Figure 5).
  262.  
  263. The nuniber of entries in a directory block varies with the length of names.
  264. If the average filename length is 1 3 characters, an average directory block
  265. will hold about 40 entries.  The entries in a directory block are sorted by
  266. the binary lexical order of their name fields (this happens to put them in
  267. alphabetical order for the U.S.  alphabet).  The last entry in a directory
  268. block is a dummy record that marks the end of the block.
  269.  
  270. When a directory gets too large to be stored in one block, it increases in
  271. size by the addition of 2Kb blocks that are organized as a B-Tree"B-Trees and
  272. B+ Trees").  When searching for a specific name, the file system traverses a
  273. directory block until it either finds a match or finds a name that is
  274. lexically greater than the target.  In the latter case, the file system
  275. extracts the BTree pointer from the entry.  If there is no pointer, the
  276. search failed; otherwise the file system follows the pointer to the next
  277. directory block in the tree and continues the search.
  278.  
  279. A little back-of-the-envelope arithmetic yields some impressive statistics.
  280. Assuming 40 entries per block, a two-level tree of directory blocks can hold
  281. 1640 directory entries and a three-level tree can hold an astonishing 65,640
  282. entries.  In other words, a particular file can be found (or shown not to
  283. exist) in a typical directory of 65,640 files with a maximum of three disk
  284. hits-the actual number of disk accesses depending on cache contents and the
  285. location of the file's name in the directory block B-Tree.  That's quite a
  286. contrast to the FAT file system, where in the worst case more than 4000
  287. sectors would have to be read to establish that a file was or was not present
  288. in a directory containing the same number of files.
  289.  
  290. The B-Tree directory structure has interesting implications beyond its effect
  291. on open and find operations.  A file creation, renaming, or deletion may
  292. result in a cascade of complex operations, as directory blocks are added or
  293. freed or names are moved from one block to the other to keep the tree
  294. balanced.  In fact, a rename operation could theoretically fail for lack of
  295. disk space even though the file itself is not growing.  In order to avoid
  296. this sort of disaster, the HPFS maintains a small pool of free blocks that
  297. can be drawn from in a directory emergency; a pointer to this pool of free
  298. blocks is stored in the SpareBlock.
  299.  
  300. Extended Attributes
  301.  
  302. File attributes are information about a file that is maintained by the
  303. operating system outside the file's overt storage area.  The FAT file system
  304. supports only a few simple attributes (read only, system, hidden, and
  305. archive) that are actually stored as bit flags in the file's directory entry;
  306. these attributes are inspected or modified by special function calls and are
  307. not accessible through the normal file open, read, and write calls.
  308.  
  309. The HPFS supports the same attributes as the FAT file system for historical
  310. reasons, but it also supports a new form of fileassociated, highly
  311. generalized information called Extended Attributes (EAs).  Each EA is
  312. conceptually similar to an environment variable, taking the form
  313.  
  314. name- -value
  315.  
  316. except that the value portion can be either a null-terminated (ASCIIZ) string
  317. or binary data. In OS/2 1.2, each file or directory can have a maximum of
  318. 64Kb of EAs attached to it.  This limit may be lifted in a later release of
  319. OS/2.
  320.  
  321. The storage method for EAs can vary.  If the EAs associated with a given file
  322. or directory are small enough, they will be stored right in the Fnode.  If
  323. the total size of the EAs is too large, they are stored outside the Fnode in
  324. sector runs, and a B+ Tree of allocation sectors can be created to describe
  325. the runs.  If a single EA gets too large, it can be pushed outside the Fnode
  326. into a B+ Tree of its own.
  327.  
  328. The kernel API functions DosQFileInfo and DosSetFileInfo have been expanded
  329. with new information levels that allow application programs to manipulate
  330. extended attributes for files.  The new functions DosQPathInfo and
  331. DosSetPathInfo are used to read or write the EAs associated with arbitrary
  332. pathnames.  An application program can either ask for the value of a specific
  333. EA (supplying a name to be matched) or can obtain all of the EAs for the file
  334. or directory at once.
  335.  
  336. Although application programs can begin to take advantage of EAs as soon as
  337. the HPFS is released, support for EAs is an essential component in
  338. Microsoft's long-range plans for object-oriented file systems, Information of
  339. almost any type can be stored in EAs, ranging from the name of the
  340. application that owns the file to names of dependent files to icons to
  341. executable code.  As the HPFS evolves, its facilities for manipulating EAs
  342. are likely to become much more sophisticated.  It's easy to imagine, for
  343. example, that in future versions the API might be extended with EA functions
  344. that are analogous to DosFindFirst and DosFindNext and EA data might get
  345. organized into B -Trees.
  346.  
  347. I should note here that in addition to EAs, the LAN Manager version of HPFS
  348. will support another class of file-associated information called Access
  349. Control Lists (ACLs).  ACLs have the same general appearance as EAs and are
  350. manipulated in a similar manner, but they are used to store access rights,
  351. passwords, and other information of interest in a networking multiuser
  352. environment.
  353.  
  354. Installable File Systems
  355.  
  356. Support for installable file system has been one of the most eagerly
  357. anticipated features of OS/2 Version 1.2.  It will make it possible to access
  358. multiple incompatible volume structures-FAT, HPFS, CD ROM, and perhaps even
  359. UNIX(R)--on the same OS/2 system at the same time, will simplify the life of
  360. network implementors, and will open the door to rapid file system evolution
  361. and innovation.  Installable file systems are, however, only relevant to the
  362. HPFS insofar as they make use of the HPFS optional.  The FAT file system is
  363. still embedded in the OS/2 kernel, as it was in OS/2 1.0 and 1.1, and will
  364. remain there as the compatibility file system for some time to come.
  365.  
  366. An installable file system driver (FSD) is analogous in many ways to a device
  367. driver.  An FSD resides on the disk in a file that is structured like a
  368. dynamic-link library (DLL), typically with a SYS or IFS extension, and is
  369. loaded during system initialization by IFS= statements in the CONFIG.SYS
  370. file.  IFS= directives are processed in the order they are encountered and
  371. are also sensitive to the order of DEVICE= statements for device drivers.
  372. This lets you load a device driver for a nonstandard device, load a file
  373. system driver from a volume on that device, and so on.
  374.  
  375. Once an FSD is installed and initialized, the kernel communicates with it in
  376. terms of logical requests for file opens, reads, writes, seeks, closes, and
  377. so on.  The FSD translates these requests-using control structures and tables
  378. found on the volume itself-into requests for sector reads and writes for
  379. which it can call special kemel entry points called File System Helpers
  380. (FsHlps).  The kemel passes the demands for sector 1/0 to the appropriate
  381. device driver and retums the results to the FSD (Figure 6).
  382.  
  383. The procedure used by the operating system to associate volumes with FSDs is
  384. called dynamic mounting and works as follows.  Whenever a volume is first
  385. accessed, or after it has been locked for direct access and then unlocked
  386. (for example, by a FORMAT operation), OS/2 presents identifying information
  387. from the volume to each of the FSDs in tum until one of them recognizes the
  388. information.  When an FSD claims the volume, the volume is mounted and all
  389. subsequent file 1/0 requests for the volume are routed to that FSD.
  390.  
  391. Performance Issues
  392.  
  393. The HPFS attacks potential bottlenecks in disk throughput at multiple levels.
  394. It uses advanced data structures, contiguous sector allocation, intelligent
  395. caching, read-ahead, and deferred writes in order to boost performance.
  396.  
  397. First, the HPFS matches its data structures to the task at hand:
  398. sophisticated data structures (B-Trees and B+ Trees) for fast random access
  399. to filenames, directory names, and lists of sectors allocated to files or
  400. directories, and simple compact data structures (bitmaps) for locating chunks
  401. of free space of the appropriate size.  The routines that manipulate these
  402. data structures are written in assembly language and have been painstakingly
  403. tuned, with special focus on the routines that search the freespace bitmaps
  404. for patterns of set bits (unused sectors).
  405.  
  406. Next, the HPFS's main goal -its prime directive, if you will- is to assign
  407. consecutive sectors to files whenever possible.  The time required to move
  408. the disk's read/write head from one track to another far outweighs the other
  409. possible delays, so the HPFS works hard to avoid or minimize such head
  410. movements by allocating file space contiguously and by keeping control
  411. structures such as Fnodes and freespace bitmaps near the things they control.
  412. Highly contiguous files also help the file system make fewer requests of the
  413. disk driver for more sectors at a time, allow the disk driver to exploit the
  414. multisector transfer capabilities of the disk controller, and reduce the
  415. number of disk completion interrupts that must be serviced.
  416.  
  417. Of course, trying to keep files from becoming fragmented in a multitasking
  418. system in which many files are being updated concurrently is no easy chore.
  419. One strategy the HPFS uses is to scatter newly created files across the
  420. disk-in separate bands, if possible-so that the sectors allocated to the
  421. files as they are extended will not be interleaved.  Another strategy is to
  422. preallocate approximately 4Kb of contiguous space to the file each time it
  423. must be extended and give back any excess when the file is closed.
  424.  
  425. If an application knows the ultimate size of a new file in advance, it can
  426. assist the file system by specifying an initial file allocation when it
  427. creates the file.  The system will then search all the free space bitmaps to
  428. find a run of consecutive sec tors large enough to hold the file.  That
  429. failing, it will search for two runs that are half the size of the file, and
  430. so on.
  431.  
  432. The HPFS relies on several different kinds of caching to minimize the number
  433. of physical disk transfers it must request.  Naturally, it caches sectors, as
  434. did the FAT file system.  But unlike the FAT file system, the HPFS can manage
  435. very large caches efficiently and adjusts sector caching on a perhandle basis
  436. to the manner in which a file is used.  The HPFS also caches pathnames and
  437. directories, transforming disk directory entries into an even more compact
  438. and efficient inmemory representation.
  439.  
  440. Another technique that the HPFS uses to improve performance is to preread
  441. data it believes the program is likely to need.  For example, when a file is
  442. opened, the file system will preread and cache the Fnode and the first few
  443. sectors of the file's contents. If the file is an executable program or the
  444. history information in the file's Fnode shows that an open operation has
  445. typically been followed by an immediate sequential read of the entire file,
  446. the file system will preread and cache much more of the file's contents.
  447. When a program issues relatively small read requests, the file system always
  448. fetches data from the file in 2Kb chunks and caches the excess, allowing most
  449. read operations to be satisfied from the cache.
  450.  
  451. Finally, the OS/2 operating system's support for multitasking makes it
  452. possible for the HPFS to rely heavity on lazy writes (sometimes called
  453. deferred writes or write behind) to improve performance.  When a program
  454. requests a disk write, the data is placed in the cache and the cache buffer
  455. is flagged as dirty (that is, inconsistent with the state of the data on
  456. disk).  When the disk becomes idle or the cache becomes saturated with dirty
  457. buffers, the file system uses a captive thread from a daemon process to write
  458. the buffers to disk, starting with the oldest data.
  459.  
  460. In general, lazy writes mean that programs run faster because their read
  461. requests will almost never be stalled waiting for a write request to
  462. complete.  For programs that repeatedly read, modify, and write a small
  463. working set of records, it also means that many unnecessary or redundant
  464. physical disk writes may be avoided.  Lazy writes have their dangers, of
  465. course, so a program can defeat them on a per-handle basis by setting the
  466. writethrough flag in the OpenMode parameter for DosOpen, or it can commit
  467. data to disk on a perhandle basis with the DosBufReset function.
  468.  
  469. Fault Tolerance
  470.  
  471. The HPFS's extensive use of lazy writes makes it imperative for the HPFS to
  472. be able to recover gracefully from write errors under any but the most dire
  473. circumstances.  After all, by the time a write is known to have failed, the
  474. application has long since gone on its way under the illusion that it has
  475. safely shipped the data into disk storage.  The errors may be detected by the
  476. hardware (such as a "sector not found" error returned by the disk adapter),
  477. or they may be detected by the disk driver in spite of the hardware during a
  478. read-after-write verification of the data.
  479.  
  480. The primary mechanism for handling write errors is called a hotfix.  When an
  481. error is detected, the file system takes a free block out of a reserved
  482. hotfix pool, writes the data to that block, and updates the hotfix map.  (The
  483. hotfix map is simply a series of pairs of doublewords, with each pair
  484. containing the number of a bad sector associated with the number of its
  485. hotfix replacement.  A pointer to the hotfix map is maintained in the
  486. SpareBlock.) A copy of the hotfix map is t written to disk, and a waming
  487. message is displayed to let the user know that all is not well with the disk
  488. device.
  489.  
  490. Each time the file system requests a sector read or write from the disk
  491. driver, it scans the hotfix map and replaces any bad sector numbers with the
  492. corresponding good sector holding the actual data. This lookaside translation
  493. of sector numbers is not as expensive as it sounds, since the hotfix list
  494. need only be scanned when a sector is physically read or written, not each
  495. time it is accessed in the cache.
  496.  
  497. One of CHKDSK's duties is to empty the hotfix map.  For each replacement
  498. block on the hotfix map, it allocates a new sector that is in a favorable
  499. location for the file that owns the data, moves the data from the hotfix
  500. block to the newly allocated sector, and updates the file's allocation
  501. information (which may involve rebalancing allocation trees and other
  502. elaborate operations).  It then adds the bad sector to the bad block list,
  503. releases the replacement sector back to the hotfix pool, deletes the hotfix
  504. entry from the hotfix map, and writes the updated hotfix map to disk.
  505.  
  506. Of course, write errors that can be detected and fixed on the fly are not the
  507. only calamity that can befall a file system.  The HPFS designers also had to
  508. consider the inevitable damage to be wreaked by power failures, program
  509. crashes, malicious viruses and Trojan horses, and those users who tum off the
  510. machine without selecting Shutdown in the Presentation Manager Shell.
  511. (Shutdown notifies the file system to flush the disk cache, update
  512. directories, and do whatever else is necessary to bring the disk to a
  513. consistent state.)
  514.  
  515. The HPFS defends itself against the user who is too abrupt with the Big Red
  516. Switch by maintaining a Dirty FS flag in the SpareBlock of each HPFS volume.
  517. The flag is only cleared when all files on the volume have been closed and
  518. all dirty buffers in the cache have been written out or, in the case of the
  519. boot volume (since OS2.INI and the swap file are never closed), when Shutdown
  520. has been selected and has completed its work.
  521.  
  522. During the OS/2 boot sequence, the file system inspects the DirtyFS flag on
  523. each HPFS volume and, if the flag is set, will not allow further access to
  524. that volume until CHKDSK has been run. If the DirtyFS flag is set on the boot
  525. volume, the system will refuse to boot; the user must boot OS/2 in
  526. maintenance mode from a diskette and run CHKDSK to check and possibly repair
  527. the boot volume.
  528.  
  529. In the event of a truly major catastrophe, such as loss of the SuperBlock or
  530. the root directory, the HPFS is designed to give data recovery the best
  531. possible chance of success.  Every type of crucial file object-including
  532. Fnodes, allocation sectors, and directory blocks-is doubly linked to both its
  533. parent and its children and contains a unique 32-bit signature.  Fnodes also
  534. contain the initial portion of the name of their file or directory.
  535. Consequently, CHKDSK can rebuild an entire volume by methodically scanning
  536. the disk for Fnodes, allocation sectors, and directory blocks, using them to
  537. reconstruct the files and directories and finally regenerating the freespace
  538. bitmaps.
  539.  
  540. Application Programs and the HPFS
  541.  
  542. Each of the OS/2 releases thus far have carried with them a major
  543. discontinuity for application programmers who learned their trade in the
  544. MS-DOS environment.  In OS/2 1.0, such programmers were faced for the first
  545. time with virtual memory, multitasking, interprocess communications, and the
  546. protected mode restrictions on addressing and direct control of the hardware
  547. and were challenged to master powerful new concepts such as threading and
  548. dynamic linking. In OS/2 Version 1.1, the stakes were raised even further.
  549. Programmers were offered a powerful hardware-independent graphical user
  550. interface but had to restructure their applications drastically for an
  551. event-driven environment based on objects and message passing.
  552.  
  553. In OS/2 Version 1.2, it is time for many of the file-oriented programming
  554. habits and assumptions carried forward from the MS-DOS environment to fall by
  555. the wayside. An application that wishes to take full advantage of the HPFS
  556. must allow for long, free-form, mixed-case filenames and paths with few
  557. restrictions on punctuation and must be sensitive to the presence of EAs and
  558. ACLs.  After all, if EAs are to be of any use, it won't suffice for
  559. applications to update a file by renaming the old file and creating a new one
  560. without also copying the EAs.
  561.  
  562. But the necessary changes for OS/2 Version 1.2 are not tricky to make.  A new
  563. API function, DosCopy, helps applications create backups-it essentially
  564. duplicates an existing file together with its EAs.  EAs can also be
  565. manipulated explicitly with DosQFileInfo, DosSetFileInfo, DosQPathInfo, and
  566. DosSetPathInfo.  A program should call DosQSysInfo at run time to find the
  567. maximum possible path length for the system, and ensure that all buffers used
  568. by DosChDir, DosQCurDir, and related functions are sufficiently large.
  569. Similarly, the buffers used by DosOpen, DosMove, D o s G e t M o d N a m e ,
  570. DosFindFirst, DosFindNext, and like functions must allow for longer
  571. filenames.  Any logic that folds cases in filenames or tests for the
  572. occurrence of only one dot delimiter in a filename must be rethought or
  573. eliminated
  574.  
  575. The other changes in the API will not affect the average application.  The
  576. functions DosQFileInfo, DosFindFirst, and DosFindNext now retum all three
  577. sets of times and dates (created, last accessed, last modified) for a file on
  578. an HPFS volume, but few programs are concerned with time and date stamps
  579. anyway.  DosQFsInfo is used to obtain volume labels or disk characteristics
  580. just as before, and the use of DosSetFsInfo for volume labels is unchanged.
  581. There are a few totally new API functions such as DosFsCtl (analogous to
  582. DosDev IOCtl but used for communication between an application and an FSD),
  583. DosFsAttach (a sort of explicit mount call), and DosQFsAttach (determines
  584. which FSD owns a volume); these are intended mainly for use by disk utility
  585. programs.
  586.  
  587. In order to prevent old OS/2 applications and MS-DOS applications running in
  588. the DOS box from inadvertently damaging HPDS files, a new flag bit has been
  589. defined in the EXE file header that indicates whether an application is
  590. HPFS-aware.  If this bit is not set, the application will only be able to
  591. search for, open, or create files on HPFS volumes that are compatible with
  592. the FAT file system's 8.3 naming conventions.  If the bit is set, OS/2 allows
  593. access to all files on an HPFS volume, because it assumes that the program
  594. knows how to handle long, free-form filenames and will take the
  595. responsibility of conserving a file's EAs and ACLs.
  596.  
  597. Summary
  598.  
  599. The HPFS solves all of the historical problems of the FAT file system .It
  600. achieves excellent throughput even in extreme cases-many very small files or
  601. a few very large files-by means of advanced data structures and techniques
  602. such as intelligent caching, read-ahead, and write-behind. Disk space is used
  603. economically because it is managed on a sector basis.  Existing application
  604. programs will need modification to take advantage of the HPFS's support for
  605. extended attributes and long filenames, but these changes will not be
  606. difficult.  All application programs will benefit from the HPFS's improved
  607. performance and decreased CPU use whether they are modified or not.  This
  608. article is based on a prerelease version of the HPFS that was still
  609. undergoing modification and tuning.  therefore, the final release of the HPFS
  610. may differ in some details from the description given here--Ed.
  611.  
  612. B- Trees and B+ Trees
  613.  
  614. Most programmers are at least passingly familiar with the data structure
  615. known as a binary tree.  Binary trees are a technique for imposing a logical
  616. ordering on a collection of data items by means of pointers, without regard
  617. to the physical order of the data.
  618.  
  619. In a simple binary tree, each node contains some data, including a key value
  620. that determines the node's logical position in the tree, as well as pointers
  621. to the node's left and right subtrees. The node that begins the tree is known
  622. as the root; the nodes that sit at the ends of the tree's branches are
  623. sometimes called the leaves.
  624.  
  625. To find a particular piece of data, the binary tree is traversed from the
  626. root, At each node, the desired key is compared with the node's key; if they
  627. don't match, one branch of the node's subtree or another is selected based on
  628. whether the desired key is less than or greater than the node's key.  This
  629. process continues until a match is found or an empty subtree is encountered
  630. (see Figure A).
  631.  
  632. Such simple binary trees, although easy to understand and implement, have
  633. disadvantages in practice.  If keys are not well distributed or are added to
  634. the tree in a non-random fashion, the tree can become quite asymmetric,
  635. leading to wide variations in tree traversal times.
  636.  
  637. In order to make access times uniform, many programmers prefer a particular
  638. type of balanced tree known as a B-Tree.  For the purposes of this
  639. discussion, the important points about a B-Tree are that data is stored in
  640. all nodes, more than one data item might be stored in a node, and all of the
  641. branches of the tree are of identical length (see Figure B).
  642.  
  643. The worst-case behavior of a B-Tree is predictable and much better than that
  644. of a simple binary tree, but the maintenance of a B-Tree is correspondingly
  645. more complex.  Adding a new data item, changing a key value, or deleting a
  646. data item may result in the splitting or merging of a node, which in tum
  647. forces a cascade of other operations on the tree to rebalance it.
  648.  
  649. A B+ Tree is a specialized form of B-Tree that has two types of nodes:
  650. internal which only point to other nodes, and external, which contain the
  651. actual data (see Figure C).
  652.  
  653. The advantage of a B+ Tree over a B- Tree is that the internal nodes of the
  654. B+ Tree can hold many more decision values than the intermediate-level nodes
  655. of a B-Tree, so the fan out of the tree is faster and the average length of a
  656. branch is shorter.  This makes up for the fact that you must always follow a
  657. B+ Tree branch to its end to get the data for which you are looking, whereas
  658. in a B-Tree you may discover the data at an intermediate node or even at the
  659. root.
  660.  
  661.  
  662. -- 
  663. Robert Kelley Cook     rcook@darwin.cc.nd.edu 
  664. Univ. of Notre Dame    
  665. Physics Dept.         If you think these are ND opinions, speak to the pope!
  666.  
  667.  
  668.